package client;
import java.applet.Applet;
import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

import java.io.*;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.text.*;
import java.util.*;

/**
 * @author Elliot Triplett
 *
 */
public class ClientGUI extends Applet implements MouseMotionListener, Runnable, ActionListener{

	private Image i; 
	private Graphics dg;
	private Image pCampusIMG;
	private Image lot2IMG;
	private Lot lot2;
	private URL url;

	int mx, my;  // the mouse coordinates
	private int width = 600;
	private int height = 800;
	private JTextField sendField;
	
	private boolean isConnected;
	ImageComponent pCampus;

	
	private Panel north;
	private Panel middle;
	private Panel login;
	private Panel mapSelect;
	private Panel map;
	
	private JTabbedPane tabbedPane;
	
	private JTextField usernameField;
	private JPasswordField passwordField;

	private JButton btnLogIn;
	private JButton btnNorthCampus;
	private JButton btnSouthCampus;
	private JButton btnSendButton;
	private JLabel label;
	private JTextField statusField;
	private JButton button;
	private JLabel label_1;
	private JTextField lotIDField;
	private JLabel label_3;
	private JLabel label_5;
	private JTextField lotCountField;
	private JTextField lotSizeField;
	
	private Client client;


	@Override
	public void init() {
		try {
			//Load Campus Map Images
			url = new URL(getCodeBase(),"imgs\\pcampus.png");
			pCampusIMG = ImageIO.read(url);
			lot2 = new Lot(2,81,25,getCodeBase()); //creates lot2 object, image position at 80,21
		} catch (IOException e) {
			e.printStackTrace();
		}
		pCampus = new ImageComponent(pCampusIMG, lot2);
		if (pCampus != null){
			width = pCampus.getPreferredSize().width;
			height = pCampus.getPreferredSize().height;
		}
		setSize(600, 800);
		setLayout(new BorderLayout());

		middle = new Panel();
		middle.addMouseMotionListener((MouseMotionListener) this);
		middle.setLayout(null);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(85, 5, 385, 188);
		middle.add(tabbedPane);

		Panel lotInfo = new Panel();
		tabbedPane.addTab("Lot Info", null, lotInfo, null);
		lotInfo.setLayout(null);
		
		label = new JLabel("Status:");
		label.setBounds(10, 43, 65, 14);
		lotInfo.add(label);
		
		statusField = new JTextField();
		statusField.setText("Disconnected");
		statusField.setHorizontalAlignment(SwingConstants.CENTER);
		statusField.setEditable(false);
		statusField.setColumns(10);
		statusField.setBounds(75, 40, 86, 20);
		lotInfo.add(statusField);
		
		button = new JButton("Refresh");
		button.setBounds(263, 71, 89, 23);
		lotInfo.add(button);
		
		label_1 = new JLabel("Lot ID:");
		label_1.setBounds(10, 18, 65, 14);
		lotInfo.add(label_1);
		
		lotIDField = new JTextField();
		label_1.setLabelFor(lotIDField);
		lotIDField.setHorizontalAlignment(SwingConstants.CENTER);
		lotIDField.setEditable(false);
		lotIDField.setColumns(10);
		lotIDField.setBounds(75, 15, 86, 20);
		lotInfo.add(lotIDField);
		
		label_3 = new JLabel("Lot Count:");
		label_3.setHorizontalAlignment(SwingConstants.LEFT);
		label_3.setBounds(10, 66, 65, 14);
		lotInfo.add(label_3);
		
		label_5 = new JLabel("Lot Size:");
		label_5.setHorizontalAlignment(SwingConstants.LEFT);
		label_5.setBounds(10, 91, 65, 14);
		lotInfo.add(label_5);
		
		lotCountField = new JTextField();
		label_3.setLabelFor(lotCountField);
		lotCountField.setColumns(10);
		lotCountField.setBounds(75, 63, 55, 20);
		lotInfo.add(lotCountField);
		
		lotSizeField = new JTextField();
		label_5.setLabelFor(lotSizeField);
		lotSizeField.setColumns(10);
		lotSizeField.setBounds(75, 88, 55, 20);
		lotInfo.add(lotSizeField);
		TextArea txtrLog = new TextArea();
		tabbedPane.addTab("Admin Log", null, txtrLog, null);
		txtrLog.setText("this is the text area..");
		tabbedPane.setEnabledAt(1,false);
		add(middle, BorderLayout.CENTER);

		btnSendButton = new JButton("Send");
		btnSendButton.setBounds(85, 199, 63, 22);
		btnSendButton.addActionListener(this);
		middle.add(btnSendButton);
		//bottom.setVisible(false);

		sendField = new JTextField();
		sendField.setBounds(150, 199, 320, 22);
		sendField.addActionListener(this);
		middle.add(sendField);
		sendField.setColumns(50);
		addMouseMotionListener(this);

		north = new Panel();
		north.setLayout(new BorderLayout(0, 0));
		north.setSize(600,800);
		
		
		add(north, BorderLayout.NORTH);
		//-END- North initialization 

		login = new Panel();
		login.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblUserName = new JLabel("User Name:");
		lblUserName.setFont(new Font("Times New Roman", Font.BOLD, 16));
		login.add(lblUserName);
		usernameField = new JTextField();
		usernameField.setText("userName");
		usernameField.setColumns(10);
		login.add(usernameField);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Times New Roman", Font.BOLD, 16));
		login.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setText("password");
		passwordField.setColumns(10);
		login.add(passwordField);
		
		btnLogIn = new JButton("Log in");
		btnLogIn.addActionListener(this);
		login.add(btnLogIn);

		north.add(login, BorderLayout.NORTH);
		//-END login initialization
		map = new Panel();
		north.add(map, BorderLayout.CENTER);

		map.add(pCampus);
		map.addMouseMotionListener(this);
		map.setSize(width, height);
		
		mapSelect = new Panel();
		north.add(mapSelect, BorderLayout.SOUTH);
		
		btnNorthCampus = new JButton("North Campus");
		mapSelect.add(btnNorthCampus);
		
		btnSouthCampus = new JButton("SouthCampus");
		btnSouthCampus.addActionListener(this);
		mapSelect.add(btnSouthCampus);
		
		middle.setVisible(false);
		map.setVisible(false);
		mapSelect.setVisible(false);
		
		client = new Client(this.getCodeBase().getHost(), lot2, this);
	}
	
	public void actionPerformed(ActionEvent a) {
		if (a.getSource() == btnLogIn){
			//Validate login
			//If validated, goto next screen
			login.setVisible(false);
			isConnected = client.connect();
			 if (isConnected){
				try {
					client.sendMessage("Connected");
				} catch (IOException e) {
					e.printStackTrace();
				}
			 }

			mapSelect.setVisible(true);
			this.validate();
			repaint();

		}
		if (a.getSource() == btnSouthCampus){
			mapSelect.setVisible(false);
			north.setSize(map.getSize());
			middle.setVisible(true);
			map.setVisible(true);
		}
		if (a.getSource() == btnSendButton || a.getSource() == sendField){
			try {
					client.sendMessage(sendField.getText());
					sendField.setText("");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		refresh();
		this.validate();
		//repaint();
	}
	
	public void refresh(){
		lotIDField.setText(String.valueOf(lot2.getLotID()));
		lotCountField.setText(String.valueOf(lot2.getLotCount()));
		lotSizeField.setText(String.valueOf(lot2.getLotSize()));
		this.repaint();
		pCampus.refresh();

	}

	@Override
	public void start() {
		//Start the thread...
		new Thread(this).start();
	}

	@Override
	public void run() {
		while (true){
			try {
				Thread.sleep(17);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			repaint();
			validate();
		}
	}

	/**
	 * Create the application.
	 */
	public ClientGUI() {

	}

	@Override
	public void update(Graphics g) {
	}

	public void paint (Graphics g){         

	}

	@Override
	public void mouseDragged(MouseEvent e) {

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mx = e.getX();
		my = e.getY();
		//showStatus( "Mouse at (" + mx + "," + my + ")" );

	}

}

class ImageComponent extends Component implements MouseListener {

	Image background;
	Image lotIMG;
	Dimension size;
	Lot lot;

	ImageComponent ( Image image, Lot lot ) {
		this.background = image;
		this.lot = lot;
		this.lotIMG = lot.getLotImage();
		MediaTracker mt = new MediaTracker(this);
		mt.addImage( image, 0 );
		try { mt.waitForAll(); } catch (InterruptedException e) { /* error */ };
		size = new Dimension ( image.getWidth(null), image.getHeight(null) );
		setSize( size );
		addMouseListener(this);
	}

	public void refresh() {
		lotIMG = lot.getLotImage();
		this.validate();
		this.repaint();
		
	}

	public void update( Graphics g ) {
		paint(g);
	}

	public void paint( Graphics g ) {
		BufferedImage screen = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
		g = screen.createGraphics();
		g.drawImage( background, 0, 0, this );
		g.drawImage(lotIMG,lot.x,lot.y,this);
		try {
				ImageIO.write(screen,"png", new File("C:\\www\\esparking\\screen.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Dimension getPreferredSize() {
		return size;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (lot.isClicked(e.getPoint())){
			//Select that lot (outline??)
		}
		repaint();
		// TODO Auto-generated method stub

	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}